package com.cz.service;

import java.io.IOException;
import java.net.*;

public class cspull {
    private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private static int       remotePort;
    private static InetAddress remoteIP;

    private static DatagramSocket socket; //UDP套接字

    public cspull(String ip, String port)throws IOException{
        this.remotePort=Integer.parseInt(port);
        this.remoteIP=InetAddress.getByName(ip);

        //创建一个UDP套接字，与本地任意一个未使用的UDP端口绑定
        socket=new DatagramSocket();
        //与本地一个固定的UDP端口绑定
        //   socket=new DatagramSocket(9000);
    }

    //定义一个数据的发送方法。
    public static void send(String msg){
        try {
            //先准备一个待发送的数据报
            //byte[] outputData=msg.getBytes();
            //构建一个数据报文。
            byte bytes[] = fromHexString(msg);
            //创建发送类型的数据包
            DatagramPacket sendPacket = new DatagramPacket(bytes,bytes.length,remoteIP,remotePort);
           // DatagramPacket outputPacket=new DatagramPacket(outputData, outputData.length,remoteIP,remotePort);
            //给EchoUDPServer发送数据报
            socket.send(sendPacket);  //给EchoUDPServer发送数据报
        } catch (IOException ex) { }
    }

    //定义一个数据的接收方法。
    public static void receive(){//throws IOException{
        //先准备一个空数据报文
        DatagramPacket inputPacket=new DatagramPacket(new byte[512],512);
        try {
            //阻塞语句，有数据就装包，以装完或装满为此.
            socket.receive(inputPacket);
            //从报文中取出字节数据并装饰成字符。
            System.out.println(inputPacket.getData());

        } catch (IOException ex) {

        }
    }

    public void close(){
        if(socket!=null)
            socket.close();//释放本地端口.
    }

    public static void main(String[] args) throws IOException {
        cspull cs = new cspull("192.168.3.112","6002");
        send("fe01010000");

                try {
                   // String sys = receive();
                        byte[] bytes = new byte[5];
                        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
                        socket.receive(dp);
                       byte[] data = dp.getData();
                       String  data1 = toHexString(data);
                        //String string = new String(data, 0,dp.getLength());
                        //if (":EXIT".equals(string))break;
                       if(data1.length()==10&&data1.startsWith("f")){
                           int have = data1.charAt(7);
                           System.out.println(data1+"----"+have);
                           if(have == 49){//asc码
                               System.out.println(have);
                           }
                       }
                        String ip = dp.getAddress().getHostAddress();
                        System.out.println(ip+"发来的信息为:"+data1);

                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
    /**
     * 方法一：将byte类型数组转化成16进制字符串
     * @explain 字符串拼接
     * @param bytes
     * @return
     */
    public static String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        int num;
        for (byte b : bytes) {
            num = b < 0 ? 256 + b : b;
            sb.append(HEX_CHAR[num / 16]).append(HEX_CHAR[num % 16]);
        }
        return sb.toString();
    }
    /**
     * 将16进制字符串转换为byte[]
     * @explain 16进制字符串不区分大小写，返回的数组相同
     * @param hexString
     *            16进制字符串
     * @return byte[]
     */
    public static byte[] fromHexString(String hexString) {
        if (null == hexString || "".equals(hexString.trim())) {
            return new byte[0];
        }

        byte[] bytes = new byte[hexString.length() / 2];
        // 16进制字符串
        String hex;
        for (int i = 0; i < hexString.length() / 2; i++) {
            // 每次截取2位
            hex = hexString.substring(i * 2, i * 2 + 2);
            // 16进制-->十进制
            bytes[i] = (byte) Integer.parseInt(hex, 16);
        }

        return bytes;
    }
}
